package org.feit.findword;

import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

import org.feit.findword.data.Score;
import org.feit.findword.data.ScoreProcessor;
import org.feit.findword.game.Game;
import org.feit.findword.trie.Solution;
import org.feit.findword.view.BoardView;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

public class MissedWords extends Activity {

	static final int BOARD_VIEW_DIALOG = 0;

	private ListView mWordsList;
	private WordsListAdapter mListAdapter;
	private AlertDialog mBoardViewDialog;
	private Game mGame;
	private BoardView mBoardView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.missed_words);
		setTitle(getString(R.string.high_scores_top));
		Bundle bun = getIntent().getExtras();
		mGame = new Game(this, bun);
		mGame.initializeDictionary();
		mWordsList = (ListView) findViewById(R.id.words_list);
		mWordsList.setEmptyView(findViewById(R.id.no_words));
		List<String> words = mGame.getWordsList();
		mListAdapter = new WordsListAdapter(this, mGame.getSolutions(), words);
		mWordsList.setAdapter(mListAdapter);
		createDialogs();
		mWordsList.setOnItemClickListener(onItemClicked);
	}
	
	final OnItemClickListener onItemClicked = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			Log.d("MissedWords", "show dialog");
			Solution solution = mListAdapter.getSolution(position);
			mBoardView.setHighlighted(solution.getMask());
			showDialog(BOARD_VIEW_DIALOG);	
			
		}

		
					
		
	};

	private void createDialogs() {
		mBoardView = new BoardView(this, mGame.getBoard());

		mBoardViewDialog = new AlertDialog.Builder(this).setTitle(
				R.string.solution).setView(mBoardView)
				.setCancelable(true).create();
		mBoardViewDialog.setCanceledOnTouchOutside(true);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		if (id == BOARD_VIEW_DIALOG) {
			return mBoardViewDialog;
		}
		return super.onCreateDialog(id);
	}
}
